extends Control

@onready var xu_button = $VBoxContainer/CharacterSelect/CharacterButtons/XuButton
@onready var ling_button = $VBoxContainer/CharacterSelect/CharacterButtons/LingButton
@onready var chen_button = $VBoxContainer/CharacterSelect/CharacterButtons/ChenButton
@onready var start_button = $VBoxContainer/StartButton
@onready var exit_button = $VBoxContainer/ExitButton

var selected_character: GameData.CharacterType = GameData.CharacterType.XU

func _ready():
	# 连接按钮信号
	xu_button.pressed.connect(_on_xu_selected)
	ling_button.pressed.connect(_on_ling_selected)
	chen_button.pressed.connect(_on_chen_selected)
	start_button.pressed.connect(_on_start_game)
	exit_button.pressed.connect(_on_exit_game)
	
	# 默认选择旭
	_update_character_selection()

func _on_xu_selected():
	selected_character = GameData.CharacterType.XU
	_update_character_selection()

func _on_ling_selected():
	selected_character = GameData.CharacterType.LING
	_update_character_selection()

func _on_chen_selected():
	selected_character = GameData.CharacterType.CHEN
	_update_character_selection()

func _update_character_selection():
	# 重置所有按钮样式
	xu_button.modulate = Color.WHITE
	ling_button.modulate = Color.WHITE
	chen_button.modulate = Color.WHITE
	
	# 高亮选中的角色
	match selected_character:
		GameData.CharacterType.XU:
			xu_button.modulate = Color.YELLOW
		GameData.CharacterType.LING:
			ling_button.modulate = Color.YELLOW
		GameData.CharacterType.CHEN:
			chen_button.modulate = Color.YELLOW

func _on_start_game():
	# 初始化游戏数据
	var game_data = GameData.get_instance()
	game_data.reset_game()
	game_data.current_character = selected_character
	
	# 根据角色设置初始属性
	match selected_character:
		GameData.CharacterType.XU:
			game_data.player_max_health = 120
			game_data.player_health = 120
			game_data.player_max_energy = 3
			game_data.player_energy = 3
		GameData.CharacterType.LING:
			game_data.player_max_health = 100
			game_data.player_health = 100
			game_data.player_max_energy = 4
			game_data.player_energy = 4
		GameData.CharacterType.CHEN:
			game_data.player_max_health = 80
			game_data.player_health = 80
			game_data.player_max_energy = 3
			game_data.player_energy = 3
	
	# 初始化角色卡组
	_initialize_character_deck()
	
	# 切换到地图场景
	get_tree().change_scene_to_file("res://scenes/MapView.tscn")

func _initialize_character_deck():
	var game_data = GameData.get_instance()
	game_data.deck.clear()
	
	match selected_character:
		GameData.CharacterType.XU:
			# 旭的初始卡组 - 高伤害攻击卡
			for i in 5:
				game_data.deck.append(AttackCard.new("strike", "打击", "基础攻击", 1, 6))
			for i in 4:
				game_data.deck.append(SkillCard.new("defend", "防御", "获得护甲", 1, "block", 5))
			game_data.deck.append(AttackCard.new("heavy_strike", "重击", "强力攻击", 2, 12))
			
		GameData.CharacterType.LING:
			# 玲的初始卡组 - 快速攻击
			for i in 6:
				game_data.deck.append(AttackCard.new("quick_strike", "快速打击", "快速攻击", 0, 4))
			for i in 3:
				game_data.deck.append(SkillCard.new("dodge", "闪避", "获得护甲", 1, "block", 3))
			game_data.deck.append(SkillCard.new("flurry", "连击", "抽牌", 1, "draw", 2))
			
		GameData.CharacterType.CHEN:
			# 澈的初始卡组 - 混合型
			for i in 4:
				game_data.deck.append(AttackCard.new("study", "学习", "基础攻击", 1, 5))
			for i in 3:
				game_data.deck.append(SkillCard.new("rest", "休息", "恢复生命", 1, "heal", 4))
			game_data.deck.append(SkillCard.new("growth", "成长", "获得能量", 0, "energy", 1))
			game_data.deck.append(AttackCard.new("exam_practice", "考试练习", "练习攻击", 2, 8))
	
	# 洗牌
	game_data.deck.shuffle()

func _on_exit_game():
	get_tree().quit()
